package com.next

import grails.validation.ValidationException
import static org.springframework.http.HttpStatus.*
import org.springframework.security.access.annotation.Secured
import grails.converters.JSON
import groovy.json.JsonOutput
import java.text.SimpleDateFormat

@Secured(['ROLE_ADMINISTRATOR'])
class AppSuggestionController
{

    AppSuggestionService appSuggestionService

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max)
    {
        params.max = 10
        params.offset = params.offset ? params.offset.toInteger() : 0
        max = 10
        def offset = params.offset
        def appSuggestionList
        def appSuggestionCount

        def createdDate = params['createdDate']

        def sql = "from AppSuggestion where createdDate >= '${createdDate}'"
        if (createdDate)
        {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            createdDate = formatter.parse(createdDate)
            appSuggestionList = AppSuggestion.findAll("from AppSuggestion where createdDate >= :createdDate", [createdDate: createdDate, max: max, offset: offset])
            appSuggestionCount = AppSuggestion.executeQuery("select count(id) from AppSuggestion where createdDate >= :createdDate", [createdDate: createdDate])[0]
        }
        else
        {
            appSuggestionList = AppSuggestion.list(params)
            appSuggestionCount = AppSuggestion.count()
        }

        respond appSuggestionList, model: [appSuggestionCount: appSuggestionCount]
    }

    def show(Long id)
    {
        respond appSuggestionService.get(id)
    }

    def create()
    {
        respond new AppSuggestion(params)
    }

    def save(AppSuggestion appSuggestion)
    {
        if (appSuggestion == null)
        {
            notFound()
            return
        }

        try
        {
            appSuggestionService.save(appSuggestion)
        }
        catch (ValidationException e)
        {
            respond appSuggestion.errors, view: 'create'
            return
        }

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'appSuggestion.label', default: 'AppSuggestion'), appSuggestion.id])
                redirect appSuggestion
            }
            '*' { respond appSuggestion, [status: CREATED] }
        }
    }

    def edit(Long id)
    {
        respond appSuggestionService.get(id)
    }

    def update(AppSuggestion appSuggestion)
    {
        if (appSuggestion == null)
        {
            notFound()
            return
        }

        try
        {
            appSuggestionService.save(appSuggestion)
        }
        catch (ValidationException e)
        {
            respond appSuggestion.errors, view: 'edit'
            return
        }

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'appSuggestion.label', default: 'AppSuggestion'), appSuggestion.id])
                redirect appSuggestion
            }
            '*' { respond appSuggestion, [status: OK] }
        }
    }

    def delete(Long id)
    {
        if (id == null)
        {
            notFound()
            return
        }

        appSuggestionService.delete(id)

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'appSuggestion.label', default: 'AppSuggestion'), id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NO_CONTENT }
        }
    }

    protected void notFound()
    {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'appSuggestion.label', default: 'AppSuggestion'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }

    def getDetail()
    {
        def id = params['id']
        def appSuggestion = AppSuggestion.findById(id)
        def attachments = Attachments.findAllByAppSuggestion(appSuggestion)
        def result = [:]
        result['cellphone'] = appSuggestion?.contact?.cellphone
        result['appName'] = appSuggestion?.appVersion?.appName
        result['type'] = appSuggestion?.type
        result['description'] = appSuggestion?.description
        result['attachments'] = attachments
        render([status: "success", result: result] as JSON)

    }
}
